using System;
using System.Collections.Generic;
using System.Text;

using System.Data;
using System.Data.Common;

using Microsoft.Practices.EnterpriseLibrary.Data;
using Microsoft.Practices.EnterpriseLibrary.Caching;

using Husb.Data;
using Husb.DataUtil;

using CentralOffice.Enterprise.Data;

using ContactTable = CentralOffice.Enterprise.Data.EnterpriseContactDataSet.EnterpriseContactsDataTable;
using ContactRow = CentralOffice.Enterprise.Data.EnterpriseContactDataSet.EnterpriseContactsRow;


namespace CentralOffice.Enterprise.DataAccess
{
    public class EnterpriseContactAdapter : DataAccessAdapter<EnterpriseContactDataSet, ContactTable, ContactRow>
    {
        public const string FetchByEnterpriseIdStoredProc = "Enterprise.EnterpriseContacts_SelectEnterpriseContactByEnterpriseId";

        public const string GetUserNameByContactIdStoredProc = "Enterprise.EnterpriseContacts_SelectEnterpriseContactUserName";
        public EnterpriseContactAdapter()
        {
            this.TableName = "EnterpriseContacts";

            GetAllStoredProc = "Enterprise.EnterpriseContacts_SelectEnterpriseContactsAll";
            InsertStoredProc = "Enterprise.EnterpriseContacts_InsertEnterpriseContact";
            UpdateStoredProc = "Enterprise.EnterpriseContacts_UpdateEnterpriseContact";
            DeleteStoredProc = "Enterprise.EnterpriseContacts_DeleteEnterpriseContact";

            GetByIdStoredProc     = "Enterprise.EnterpriseContacts_SelectEnterpriseContact";
            GetByNameStoredProc = "";
            GetPagedStoredProc    = "Enterprise.EnterpriseContacts_SelectEnterpriseContactsPaged";
            GetRowCountStoredProc = "Enterprise.EnterpriseContacts_SelectEnterpriseContactsRowCount";

            FindStoredProc = "Enterprise.EnterpriseContacts_SelectNoticeReceiversDynamic";
        }

        protected override void PopulateInsertParameters(Database db, DbCommand cmd)
        {
            db.AddInParameter(cmd, "Id", DbType.Guid, "Id", DataRowVersion.Current);
            db.AddInParameter(cmd, "EnterpriseId", DbType.Guid, "EnterpriseId", DataRowVersion.Current);
            db.AddInParameter(cmd, "IsPrimary", DbType.Boolean, "IsPrimary", DataRowVersion.Current);
            db.AddInParameter(cmd, "Name", DbType.String, "Name", DataRowVersion.Current);
            db.AddInParameter(cmd, "QueryCode", DbType.AnsiString, "QueryCode", DataRowVersion.Current);
            db.AddInParameter(cmd, "ContactNumber", DbType.String, "ContactNumber", DataRowVersion.Current);
            db.AddInParameter(cmd, "BirthDate", DbType.DateTime, "BirthDate", DataRowVersion.Current);
            db.AddInParameter(cmd, "Gender", DbType.AnsiString, "Gender", DataRowVersion.Current);
            db.AddInParameter(cmd, "Nation", DbType.AnsiString, "Nation", DataRowVersion.Current);
            db.AddInParameter(cmd, "Nationality", DbType.AnsiString, "Nationality", DataRowVersion.Current);
            db.AddInParameter(cmd, "IDType", DbType.AnsiString, "IDType", DataRowVersion.Current);
            db.AddInParameter(cmd, "IDNumber", DbType.AnsiString, "IDNumber", DataRowVersion.Current);
            db.AddInParameter(cmd, "MaritalStatus", DbType.AnsiString, "MaritalStatus", DataRowVersion.Current);
            db.AddInParameter(cmd, "HealthStatus", DbType.AnsiString, "HealthStatus", DataRowVersion.Current);
            db.AddInParameter(cmd, "EducationLevel", DbType.AnsiString, "EducationLevel", DataRowVersion.Current);
            db.AddInParameter(cmd, "PoliticalFace", DbType.AnsiString, "PoliticalFace", DataRowVersion.Current);
            db.AddInParameter(cmd, "School", DbType.String, "School", DataRowVersion.Current);
            db.AddInParameter(cmd, "HireDate", DbType.DateTime, "HireDate", DataRowVersion.Current);
            db.AddInParameter(cmd, "Title", DbType.String, "Title", DataRowVersion.Current);
            db.AddInParameter(cmd, "Email", DbType.String, "Email", DataRowVersion.Current);
            db.AddInParameter(cmd, "Email2", DbType.String, "Email2", DataRowVersion.Current);
            db.AddInParameter(cmd, "IMType", DbType.AnsiString, "IMType", DataRowVersion.Current);
            db.AddInParameter(cmd, "IMID", DbType.String, "IMID", DataRowVersion.Current);
            db.AddInParameter(cmd, "WorkPhone", DbType.String, "WorkPhone", DataRowVersion.Current);
            db.AddInParameter(cmd, "Extension", DbType.String, "Extension", DataRowVersion.Current);
            db.AddInParameter(cmd, "Telephone1", DbType.String, "Telephone1", DataRowVersion.Current);
            db.AddInParameter(cmd, "Telephone2", DbType.String, "Telephone2", DataRowVersion.Current);
            db.AddInParameter(cmd, "Address", DbType.String, "Address", DataRowVersion.Current);
            db.AddInParameter(cmd, "PostalCode", DbType.AnsiString, "PostalCode", DataRowVersion.Current);
            db.AddInParameter(cmd, "Native", DbType.String, "Native", DataRowVersion.Current);
            db.AddInParameter(cmd, "Status", DbType.AnsiString, "Status", DataRowVersion.Current);
            db.AddInParameter(cmd, "PhotoPath", DbType.AnsiString, "PhotoPath", DataRowVersion.Current);
            db.AddInParameter(cmd, "IsDeleted", DbType.Boolean, "IsDeleted", DataRowVersion.Current);
            db.AddInParameter(cmd, "IsActive", DbType.Boolean, "IsActive", DataRowVersion.Current);
            db.AddInParameter(cmd, "CreatedTime", DbType.DateTime, "CreatedTime", DataRowVersion.Current);
            db.AddInParameter(cmd, "CreatedBy", DbType.Guid, "CreatedBy", DataRowVersion.Current);
            db.AddInParameter(cmd, "ModifiedTime", DbType.DateTime, "ModifiedTime", DataRowVersion.Current);
            db.AddInParameter(cmd, "LastModifiedBy", DbType.Guid, "LastModifiedBy", DataRowVersion.Current);
            db.AddInParameter(cmd, "Version", DbType.Int32, "Version", DataRowVersion.Current);
            db.AddInParameter(cmd, "Description", DbType.String, "Description", DataRowVersion.Current);
        }

        public ContactTable FetchByEnterpriseId(Guid enterpriseId)
        {
            List<DatabaseParameter> parameters = new List<DatabaseParameter>();
            parameters.Add(new DatabaseParameter("EnterpriseId", DbType.Guid, enterpriseId));

            return this.GetTable(FetchByEnterpriseIdStoredProc, parameters, true, false, null);
        }

        public string GetUserNameByContactId(Guid contactId)
        {
            List<DatabaseParameter> parameters = new List<DatabaseParameter>();
            parameters.Add(new DatabaseParameter("ContactId", DbType.Guid, contactId));

            object o = Husb.DataUtil.DataAccessUtil.GetValue(GetUserNameByContactIdStoredProc, parameters, true);
            return o == null ? (string)o : null;
        }


    }
}
